﻿#include "databasemanager.h"

namespace Common {

DatabaseManager::DatabaseManager(QObject *parent)
    : QObject(parent)
{

}

DatabaseManager::~DatabaseManager()
{

}

void DatabaseManager::setDbParameter(DatabaseParameter info)
{
    m_databaseConfigInfo = info;
}

DatabaseParameter DatabaseManager::getDbParameter()
{
    return m_databaseConfigInfo;
}

DatabaseLink *DatabaseManager::createDatabase(QString name)
{
    DatabaseLink * pDbLink = new DatabaseLink(m_databaseConfigInfo.dbType, name);
    do
    {
        if (!pDbLink->init())
            break;

        if (!pDbLink->open(m_databaseConfigInfo.hostName, m_databaseConfigInfo.dbName, m_databaseConfigInfo.dbUser, m_databaseConfigInfo.password, m_databaseConfigInfo.port))
            break;
        return pDbLink;

    } while (0);
    delete pDbLink;
    return nullptr;
}

bool DatabaseManager::hasFeature(QSqlDriver::DriverFeature feature)
{
    static DatabaseLink db(m_databaseConfigInfo.dbType);
    if (db.init())
    {
        return db.sqlDatabase().driver()->hasFeature(feature);
    }
    return false;
}

QStringList DatabaseManager::availableDrivers()
{
    return QSqlDatabase::drivers();
}

}//namespace Database
